package guicommon.controls.handlers;

import guicommon.Body;
import guicommon.Header;
import guicommon.Message;
import java.nio.CharBuffer;

public class EventMessage extends Message {

    public static final Integer ID = 0;

    public static final String SENDER = "sender";

    public static final String ARGUMENTS = "args";

    protected EventMessage(Header header, CharBuffer buffer) {
        super(header, buffer);
    }

    public EventMessage(String id, EventArgs args) {
        super(Header.FromType(EventMessage.class, ID));
        SetBodyField(SENDER, id);
        SetBodyField(ARGUMENTS, args.ToString());
    }

    @Override
    protected void ParseBody(CharBuffer charBuffer) {
        String allPropertiesString = charBuffer.toString();
        String[] properties = allPropertiesString.split(Body.SOH);
        for (String current : properties) {
            String[] pair = current.split("=");
            if (pair.length != 1) {
                AddParseBodyField(pair[0], pair[1]);
            }
        }
    }

    public String GetSender() {
        return GetBodyField(SENDER);
    }

    public String GetEventArgs() {
        return GetBodyField(ARGUMENTS);
    }
}
